package org.leafstar.shortlink.project.controller;

import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import lombok.RequiredArgsConstructor;
import org.leafstar.shortlink.project.common.convention.result.Result;
import org.leafstar.shortlink.project.common.convention.result.Results;
import org.leafstar.shortlink.project.dto.req.ShortLinkBatchCreateReqDTO;
import org.leafstar.shortlink.project.dto.resp.ShortLinkBatchCreateRespDTO;
import org.leafstar.shortlink.project.service.LinkService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
public class CoreRedirectController {
    private final LinkService linkService;

    @PostMapping("/api/short-link/v1/create/batch")
    public Result<ShortLinkBatchCreateRespDTO> batchCreateShortLink(@RequestBody ShortLinkBatchCreateReqDTO requestParam) {
        return Results.success(linkService.batchCreateShortLink(requestParam));
    }

    /**
     * 短链接重定向，即核心跳转功能
     * @param shortUri suffix
     * @param request 请求
     * @param response 返回
     */
    @GetMapping("/{short-uri}")
    public void restoreUrl(@PathVariable("short-uri") String shortUri, ServletRequest request, ServletResponse response){
        linkService.restoreUrl(shortUri,request,response);
    }
}
